'use strict';
const dayjs = require("dayjs");

module.exports = app => {
    const { INTEGER, STRING, DATE, TINYINT } = app.Sequelize;
    const LogisticsTrack = app.model.define(
        'LogisticsTrack',
        {
            id: { type: INTEGER, primaryKey: true, autoIncrement: true },
            order_id: { type: INTEGER, allowNull: false, comment: '订单ID' },
            track_status: { type: TINYINT(1), allowNull: false, comment: '轨迹状态：0-已揽收，1-运输中，2-派送中，3-已签收，4-异常' },
            track_info: { type: STRING(255), allowNull: false, comment: '轨迹信息' },
            track_location: { type: STRING(255), allowNull: true, comment: '轨迹位置' },
            operator: { type: STRING(50), allowNull: true, comment: '操作人' },
            create_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            update_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            }
        },
        {
            tableName: 'logistics_track',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );

    LogisticsTrack.associate = function() {
        app.model.LogisticsTrack.belongsTo(app.model.ExpressOrder, { foreignKey: 'order_id', targetKey: 'id', as: 'order' });
    };

    return LogisticsTrack;
};
